package com.nt.mes_leader.utils;

import android.os.Environment;
import android.text.TextUtils;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by liushuanglong on 2017/9/12.
 */
public class StringUtils {

    private static final String TAG = "StringUtils";

    public static SimpleDateFormat infoFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //24小时制
    public static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  // 转换成年、月、日
    public static SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss"); // 转换成时间

    /**
     * 获取sd卡路径
     *
     * @return
     */
    public static String getSDPath() {
        File sdDir = null;
        boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);//判断sd卡是否存在
        if (sdCardExist) {
            sdDir = Environment.getExternalStorageDirectory();//获取跟目录
//            return sdDir.toString();
            return sdDir.getAbsolutePath();
        }
        return null;
    }

    /**
     * 确定是否是时间戳
     * 仅判断10位数字
     *
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) {
        boolean isCurNum = false;
        if (!TextUtils.isEmpty(str) && ((str.length() == 10) || (str.length() == 13))) {
            Pattern pattern = Pattern.compile("[0-9]*");
            Matcher isNum = pattern.matcher(str);
            if (isNum.matches()) {
                isCurNum = true;
            }
        }
        return isCurNum;
    }

    /**
     * 轉成10位的時間戳
     *
     * @param time
     * @return
     */
    public static long toTenNumeric(long time) {
        String strTime = String.valueOf(time);
        if (!TextUtils.isEmpty(strTime) && (strTime.length() == 13)) {
            time = time / 1000;
        }
        return time;
    }

    /**
     * long类型转时间字符串
     *
     * @param time
     * @return
     */
    public static String numericToStr(long time) {
        time = (String.valueOf(time).length() == 10) ? time * 1000 : time;
        return infoFormat.format(new Date(time));
    }

    /**
     * 时间戳转时间
     *
     * @param s
     * @return
     */
    public static String numericToStr(String s) {
        if (isNumeric(s)) {
            long cur = Long.valueOf(s);
            return infoFormat.format(new Date((s.length() == 10) ? cur * 1000 : cur));
        }
        return s;
    }

    /**
     * 时间戳转日期
     *
     * @param s
     * @return
     */
    public static String numericToDate(String s) {
        if (isNumeric(s)) {
            long cur = Long.valueOf(s);
            return dateFormat.format(new Date((s.length() == 10) ? cur * 1000 : cur));
        }
        return s;
    }

    /**
     * 计算time2减去time1的差值 差值只设置 几天 几个小时 或 几分钟
     * 根据差值返回多长之间前或多长时间后
     *
     * @param time1
     * @param time2
     * @return
     */
    public static String getDistanceTime(long time1, long time2) {
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        long diff;
        String flag;
        if (time1 < time2) {
            diff = time2 - time1;
            flag = "前";
        } else {
            diff = time1 - time2;
            flag = "后";
        }
        day = diff / (24 * 60 * 60 * 1000);
        hour = (diff / (60 * 60 * 1000) - day * 24);
        min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
        sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        if (day != 0) return day + "天" + flag;
        if (hour != 0) return hour + "小时" + flag;
        if (min != 0) return min + "分钟" + flag;
        return "刚刚";
    }

    public static String getDistanceTime1(long start, long end) {
        String distance = "";
        long diff;
        if (end > start) {
            diff = end - start;
        } else {
            diff = start - end;
        }
        long day = diff / (24 * 60 * 60);
        long hour = (diff / (60 * 60) - day * 24);
        long min = ((diff / (60)) - day * 24 * 60 - hour * 60);
        long sec = (diff - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        LogUtils.i(TAG, " getDistanceTime1 --- diff == " + diff + " , day == " + day + " , hour == " + hour + " , min == " + min + " , sec == " + sec);
        if (day > 0) {
            distance += (day + "天");
        }
        if (hour > 0) {
            distance += (hour + "小时");
        }
        if (min > 0) {
            distance += (min + "分");
        }
        return distance;
    }
}

